<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>内容获取与渲染</title>
</head>
<body>
    <div id="root"></div>
    <template>
        <div>{{count}}</div>
        <div>{{name}}</div>
    </template> 
</body>
<script>
    const tpl = document.querySelector('template');
    const root = document.querySelector('#root');
    const name = 'xiaoming';
    const count = 0;
    // 正则表达式定义
    const telReg =/\{\{.+?\}\}/g; //匹配 {{xx}} 整个内容
    const contentReg = /(?<=(\{\{)).+?(?=(\}\}))/g // 匹配{{}}里面的xx
    // 新增一个转换函数
    const replaceTemplate = (tplStr) => {
        return tplStr.replace(telReg, (item)=>{
            const content = item.match(contentReg)[0];
            return eval(content);
        })
    }
    // 替换template的内容
    tpl.innerHTML = replaceTemplate(tpl.innerHTML);
    console.dir(tpl)
    root.appendChild(tpl.content);
</script>
</html>